package com.example.hyg_veintype.activity.account.login;

import android.animation.ArgbEvaluator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.activity.web.WebViewActivity;
import com.example.hyg_veintype.base.BaseVeinTypeActivity;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.sweetalertdialog.SweetAlertDialog;

import java.util.ArrayList;
import java.util.List;



import static com.hyg.lib_common.constant.UrlUtils.PrivacyUrl;

/**
 * @author: yyw
 * time: 2020/12/21 15:36
 * desc:登录页
 */
public class LoginActivity extends BaseVeinTypeActivity{


    TextView text1;
    TextView text2;
    TextView line1;
    TextView line2;
    ViewPager2 viewFlipper1;
    CheckBox CBAgreement;
    TextView Agreement;


    LoginVpAdapter vpAdapter;
    SweetAlertDialog pDialog;//隐私条款对话框
    private int PrivacyBuilding = -1;


    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.veintype_activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        text1 = findViewById(R.id.text1);
        text1.setOnClickListener(this);
        text2 = findViewById(R.id.text2);
        text2.setOnClickListener(this);
        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        viewFlipper1 = findViewById(R.id.viewFlipper1);
        CBAgreement = findViewById(R.id.CB_Agreement);
        CBAgreement.setOnClickListener(this);
        Agreement = findViewById(R.id.Agreement);
        Agreement.setOnClickListener(this);


        tvTitle.setText("登录");
        tvTitle.setTextColor(ContextCompat.getColor(this, R.color.MainThemeColor));
        lnTopLeft.setVisibility(View.GONE);
        initVpAdapter();
        //第一次进入弹出隐私条款
        popup_privacy_web();
        Agreement.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线

    }

    private void initVpAdapter() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        vpAdapter = new LoginVpAdapter(list, this, progressDialog);
        viewFlipper1.setAdapter(vpAdapter);
        viewFlipper1.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                if (position == 0) {
                    ArgbEvaluator evaluator = new ArgbEvaluator(); // ARGB求值器
                    text1.setTextColor((Integer) evaluator.evaluate(positionOffset, 0XFF7b40be, 0X88000000));
                    line1.setBackgroundColor((Integer) evaluator.evaluate(positionOffset, 0XFFD0C1EB, 0X00FFFFFF));

                    text2.setTextColor((Integer) evaluator.evaluate(positionOffset, 0X88000000, 0XFF7b40be));
                    line2.setBackgroundColor((Integer) evaluator.evaluate(positionOffset, 0X00FFFFFF, 0XFFD0C1EB));
                }
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:// 将页面1设置成白色
                        text1.setTextColor(getResources().getColor(R.color.MainThemeColor));
                        text2.setTextColor(getResources().getColor(R.color.black));
                        line1.setBackgroundColor(getResources().getColor(R.color.gradient_Violet));
                        line2.setBackground(null);

                        break;
                    case 1:// 将页面2设置成白色
                        text1.setTextColor(getResources().getColor(R.color.black));
                        text2.setTextColor(getResources().getColor(R.color.MainThemeColor));
                        line1.setBackground(null);
                        line2.setBackgroundColor(getResources().getColor(R.color.gradient_Violet));
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.text1) {
            viewFlipper1.setCurrentItem(0, true);
        } else if (id == R.id.text2) {
            viewFlipper1.setCurrentItem(1, true);
        } else if (id == R.id.Agreement) {
            show_privacy_dialog();
        } else if (id == R.id.CB_Agreement) {
            show_privacy_dialog();
        }
    }

    public void popup_privacy_web() {
        /*  检查是否有必要 弹出一页 隐私政策 */
        if (!Utils.isEmpty(ssu.getInt("PrivacyBuild", -1))) {
            if (ssu.getInt("PrivacyBuild", -1) < PrivacyBuilding) {
//                editorn = ssu.edit();
//                editorn.putInt("PrivacyBuild",PrivacyBuilding);
//                editorn.commit();
//                Intent_privacy();
                show_privacy_dialog();
            }
        }
    }

    /**
     * 隐私条款
     */
    public void show_privacy_dialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.veintype_dialog_privacy, null);

        final TextView JumpPrivacyText = view.findViewById(R.id.PrivacyName);
        JumpPrivacyText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    return;
                }
                WebViewActivity.start(LoginActivity.this, PrivacyUrl, "隐私政策与免责声明", null);

            }
        });
        pDialog = new SweetAlertDialog(this, SweetAlertDialog.NORMAL_TYPE);
        pDialog.setTitleText("隐私政策与免责声明");
        pDialog.setCustomView(view);
        pDialog.setConfirmButton("我同意", new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
                CBAgreement.setChecked(true);
                editorn = ssu.edit();
                editorn.putInt("PrivacyBuild", PrivacyBuilding);
                editorn.commit();
            }
        });
        pDialog.setCancelButton("暂不使用", new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
                editorn = ssu.edit();
                editorn.putInt("PrivacyBuild", -1);
                editorn.commit();
                finishAffinity();
            }
        });
        pDialog.setCancelable(false);
        pDialog.show();
    }
}
